import inquirer from "inquirer";
import ora from "ora";
import shelljs from "shelljs";

let errorList = [];
let totalCount = 0;
let successCount = 0;
let errorMessage = "";
const uploadAnimation = ora("上传中...");
// 命令列表
// 读取文件夹列表
export const readUploadDir = () => {
    uploadAnimation.start();
    const path = "./";
    errorList = [];
    shelljs.ls(path).forEach((folder, index) => {
        totalCount++;
        const packagePath = `${path}/${folder}/package.json`;
        if (shelljs.test("-d", folder) && shelljs.test("-f", packagePath)) {
            shelljs.cd(`${path}/${folder}`);
            try {
                const result = shelljs.exec("npm publish", { silent: false });
                if (result.code === 0) {
                    console.log(`${folder}包上传成功 *****`);
                    successCount++;
                } else {
                    console.log(`${folder}包上传失败 *****`);
                    if (index == 0) {
                        errorMessage = result.stderr;
                    }
                    // console.log(result.stderr);
                    errorList.push(folder);
                }
            } catch (error) {
                console.log(`${folder}包上传过程中捕获到错误`, error);
            } finally {
                shelljs.cd("../");
            }
        } else {
            console.log(`${folder}文件夹不存在或package.json文件不存在  *****`);
            errorList.push(folder);
        }
    });
    queryRepeatUpload();
};
const queryRepeatUpload = () => {
    if (errorList.length) {
        let str = "";
        errorList.forEach((item) => {
            str += `${item}  `;
        });
        console.log(errorMessage);
        console.log(`*****上传总数：`, totalCount);
        console.log(`*****成功数：`, successCount);
        console.log(`*****失败数：`, totalCount - successCount);
        console.log(`*****失败包：`, str);
        uploadAnimation.stop();
        inquirer
            .prompt([
                {
                    type: "confirm",
                    name: "repeat",
                    message: "是否继续上传失败的包？",
                    default: false,
                },
            ])
            .then((answer) => {
                if (answer.repeat) {
                    errorCallback();
                } else {
                    console.log("上传结束");
                }
            });
    } else {
        console.log("上传完成");
        uploadAnimation.succeed("上传完毕");
    }
};
const errorCallback = () => {
    uploadAnimation.start();
    const path = "./";
    const count = errorList.length;
    for (let i = 0; i < count; i++) {
        const folder = errorList[i];
        const packagePath = `${path}/${folder}/package.json`;
        if (shelljs.test("-d", folder) && shelljs.test("-f", packagePath)) {
            shelljs.cd(`${path}/${folder}`);
            try {
                const result = shelljs.exec("npm publish", { silent: false });
                if (result.code === 0) {
                    console.log(`${folder}包上传成功  *****`);
                    successCount++;
                    errorList.splice(i, 1);
                } else {
                    // console.log(`${folder}包上传失败`);
                    if (i == 0) {
                        errorMessage = result.stderr;
                    }
                    // console.log(result.stderr);
                }
            } catch (error) {
                console.log(`${folder}包上传过程中捕获到错误`, error);
            } finally {
                shelljs.cd("../");
            }
        } else {
            console.log(`${folder}文件夹不存在或package.json文件不存在  *****`);
        }
    }
    queryRepeatUpload();
};
export default readUploadDir;
